﻿using UnityEngine;

namespace UBlockly.UI
{
    [RequireComponent(typeof(RectTransform))]
    public sealed class NSlotPair : MonoBehaviour
    {
        [SerializeField]
        private long id;
        [SerializeField]
        private RectTransform upRect;
        [SerializeField]
        private RectTransform downRect;
        public RectTransform ARect => upRect;
        public RectTransform BRect => downRect;
        public long NodeId => id;

        public void InitPair(int count)
        {
            RectTransform slotInfo = GetComponent<RectTransform>();
            slotInfo.anchorMin = Vector2.zero;
            slotInfo.anchorMax = Vector2.one;
            slotInfo.offsetMin = Vector2.zero;
            slotInfo.offsetMax = Vector2.zero;
            GameObject upsoltInfo = new GameObject("SlotInfo-Upper");
            upsoltInfo.transform.SetParent(transform);
            if (count == 2)
            {
                upRect = upsoltInfo.AddComponent<RectTransform>();
                upRect.anchorMin = new Vector2(0, 1);
                upRect.anchorMax = new Vector2(0, 1);
                upRect.pivot = Vector2.zero;
                upRect.offsetMin = Vector2.zero;
                upRect.sizeDelta = new Vector2(120, 50);
            }
            GameObject downslotInfo = new GameObject("SlotInfo-Down");
            downRect = downslotInfo.AddComponent<RectTransform>();
            downRect.SetParent(transform);
            downRect.anchorMin = Vector2.zero;
            downRect.anchorMax = Vector2.zero;
            downRect.offsetMax = Vector2.zero;
            downRect.offsetMin = Vector2.zero;
            downRect.pivot = new Vector2(0, 1);
            downRect.sizeDelta = new Vector2(120, 50);
        }

        //public NSlotPair(Transform up, Transform down)
        //{
        //    upRect = downRect = null;
        //    if (up != null && up.GetComponent<RectTransform>() != null)
        //        upRect = up.GetComponent<RectTransform>();
        //    if (down != null && down.GetComponent<RectTransform>() != null)
        //        downRect = down.GetComponent<RectTransform>();
        //}

        public void SetBelongId(long id)
        {
            this.id = id;
        }

        public void ActiveARect(bool enable)
        {
            if (upRect != null)
            {
                upRect.gameObject.SetActive(enable);
            }
        }
        public void ActiveBRect(bool enable)
        {
            if (downRect != null)
            {
                downRect.gameObject.SetActive(enable);
            }
        }
        public void MuteAllRect(bool mute = true)
        {
            ActiveARect(!mute);
            ActiveBRect(!mute);
        }

        /// <summary>
        /// 0,没接上，1在上，2在下
        /// </summary>
        /// <param name="anther"></param>
        /// <returns></returns>
        public int IsSlotPossible(NSlotPair anther)
        {
            if (anther != null)
            {
                if (upRect.gameObject.activeSelf && anther.downRect.gameObject.activeSelf)
                {
                    Debug.Log("yiiiii");
                    if (UBlockExtension.IsRecttransAcross(upRect, anther.downRect))
                        return 2;
                }
                if (downRect.gameObject.activeSelf && anther.upRect.gameObject.activeSelf)
                {
                    if (UBlockExtension.IsRecttransAcross(downRect, anther.upRect))
                        return 1;
                }
            }
            return 0;
        }
    }
}
